package org.sky.service.serviceImpl;

import org.sky.base.DaoSupport;
import org.sky.model.Booking;
import org.sky.model.Person;
import org.sky.model.State;
import org.sky.service.BookingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BookingServiceImpl extends DaoSupport<Booking> implements BookingService {

    public List<Booking> findBookingByCreateDate(Date begin, Date end) {

        Query query = em.createQuery("from Booking b where b.createDate >= ?1 and b.createDate <= ?2");
        query.setParameter(1, begin).setParameter(2, end);
        return query.getResultList();
    }

    /**
     * 输出全部预约订单
     *
     * @return
     */
    public List<Booking> allBooking() {
        Query query = em.createQuery("from Booking b ");
        return query.getResultList();
    }

    /**
     * 同一用户相同唱k时间内的订单
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param name      用户名
     * @return
     */
    //Todo: Nickle doesn't very understand the corresponding business
    // And he want to ask why you need to write so many query sql which cover all the time?
    // It look some kind of weird.
    public boolean findBookingByBeginDate(Date beginTime, Date endTime, String name, Person person) {
        Query[] query = new Query[4];
        query[0] = em.createQuery("from Booking b where b.beginTime >= ?1 and b.endTime <= ?2 and b.name = ?3 and b.userId = ?4");
        query[1] = em.createQuery("from Booking b where b.beginTime <= ?1 and b.endTime >= ?1 and b.endTime >= ?2 and b.name = ?3 and b.userId = ?4");
        query[2] = em.createQuery("from Booking b where b.beginTime > ?1 and b.beginTime < ?2 and b.endTime > ?2 and b.name = ?3 and b.userId = ?4");
        query[3] = em.createQuery("from Booking b where b.beginTime < ?1 and b.endTime > ?1 and b.endTime < ?2 and b.name = ?3 and b.userId = ?4");
        for (int i = 0; i < query.length; i++) {
            query[i].setParameter(1, beginTime).setParameter(2, endTime).setParameter(3, name).setParameter(4, person);
            if (query[i].getResultList().size() > 0)
                return true;
        }
        return false;
    }


    /**
     * 查找未审核的订单
     *
     * @return
     */
    public List<Booking> findNotAuditOrder() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", State.NOT_AUDIT);
        return this.findByFields(Booking.class, params);
    }

    /**
     * 按照订单编号查找订单
     *
     * @param orderId 订单编号
     * @return
     */
    public Booking findOrderByOrderId(String orderId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderId", orderId);
        Booking booking = this.findSpecialObject(Booking.class, params);
        if (booking != null)
            return booking;
        else {
            params.put("tel", orderId);
            params.put("state", State.AUDIT);
            return this.findByFields(Booking.class, params).size() > 0 ? this.findByFields(Booking.class, params).get(0) : null;
        }
    }

    @Override
    public List<Booking> findBookingByMeg(String message, int personId) {
        Query query = em.createQuery("from Booking b where (b.orderId like '%" + message + "%' or b.tel like '%" + message + "%' or b.name like '%" + message + "%') and b.userId.id = ?1");
        query.setParameter(1, personId);
        return query.getResultList();
    }
}
